﻿using Commons.Responses;
using DomainCommons.Models;
using EntityModels.E05.Reports;
using IRepositories;
using Microsoft.EntityFrameworkCore;
using ORM;
using Repositories.Helpers;
using System.Linq;
using System.Linq.Expressions;

namespace Repositories
{
    public class PhotographReportRepository:BaseRepository<int,Report>, IPhotographReportRepository
    {
        public PhotographReportRepository(PSPDbContext context) : base(context)
        {
        }

        public override async Task<DatasResponse<Report>> GetBySelectAsync(int pageIndex, int pageSize, Expression<Func<Report, bool>> predicate)
        {
            IQueryable<Report> dbSet = Context!.Set<Report>().Include(x=>x.Reporter).Include(x=>x.ReportType).Include(x=>x.Photograph).Include(x=>x.Photograph.AppUser);
            if (pageIndex == 0)
            {
                pageIndex = 1;
            }
            dbSet = dbSet.OrderByDescending(x => x.CreationTime);
            if (predicate != null)
            {
                if (pageSize < 1)
                    pageSize = 100;
                var datas = await dbSet.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
                var total = await dbSet.Where(predicate).CountAsync();
                return new DatasResponse<Report>(total, datas);
            }
            else
            {
                if (pageSize < 1)
                    pageSize = 100;
                var datas = await dbSet.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
                var total = await dbSet.CountAsync();
                return new DatasResponse<Report>(total, datas);
            }
        }
    }
    
}
